package com.insigma.business.qrcode.job._bff.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.insigma.business.qrcode.common.template.BasePagingResponse;
import com.insigma.business.qrcode.common.template.area.BaseProviderImpl;
import com.insigma.business.qrcode.common.template.area.ScenarioCodeEnum;
import com.insigma.business.qrcode.common.template.area.ServiceCallback;
import com.insigma.business.qrcode.common.util.BizCheckUtil;
import com.insigma.business.qrcode.common.util.RpcContextUtil;
import com.insigma.business.qrcode.common.util.SessionUtils;
import com.insigma.business.qrcode.common.vo.BaseDeleteRequest;
import com.insigma.business.qrcode.common.vo.BaseResponse;
import com.insigma.business.qrcode.common.vo.SessionSoldier;
import com.insigma.business.qrcode.common.vo.UserTypeEnum;
import com.insigma.business.qrcode.intell._provider.UserProvider;
import com.insigma.business.qrcode.job._bff.JobFairOrgParticipateBFFProvider;
import com.insigma.business.qrcode.job._provider.JobFairParticipateProvider;
import com.insigma.business.qrcode.job.consumer.JobRecruitPositionConsumer;
import com.insigma.business.qrcode.job.dto.FairModeEnum;
import com.insigma.business.qrcode.job.dto.JobCheckStatusEnum;
import com.insigma.business.qrcode.job.dto.JobFairParticipateDTO;
import com.insigma.business.qrcode.job.dto.request.JobFairParticipateInsertRequest;
import com.insigma.business.qrcode.job.dto.request.JobFairParticipateSearchRequest;
import com.insigma.business.qrcode.job.dto.request.JobFairParticipateUpdateRequest;
import com.insigma.business.qrcode.job.dto.request.JobRecruitPositionInsertRequest;
import com.insigma.business.qrcode.soldier._provider.CompanyProvider;
import com.insigma.business.qrcode.soldier.dto.CompanyDTO;
import com.insigma.business.qrcode.soldier.dto.OrgTypeEnum;
import com.insigma.business.qrcode.soldier.request.BaseGetRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Slf4j
@Service
@Component("jobFairParticipateBffProvider")
public class JobFairParticipateBFFProviderImpl extends BaseProviderImpl implements JobFairOrgParticipateBFFProvider {

    @Autowired
    private JobRecruitPositionConsumer jobRecruitPositionConsumer;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private JobFairParticipateProvider jobFairParticipateProvider;

    @Autowired
    private CompanyProvider companyProvider;

    @Override
    public BasePagingResponse<List<JobFairParticipateDTO>> searchJobFairOrgParticipate(final JobFairParticipateSearchRequest searchRequest) {

        final BasePagingResponse<List<JobFairParticipateDTO>> basePagingResponse = new BasePagingResponse<>();
        serviceTemplate.execute(basePagingResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(searchRequest, "查询分页JobFairParticipate的请求参数不能为空");
            }

            @Override
            public void executeService() {
                SessionSoldier sessionSoldier = RpcContextUtil.getSessionSoldier();
                if (sessionSoldier != null) {
                    searchRequest.setAuditStatus(JobCheckStatusEnum.SUCCESS.name());
                }

                BasePagingResponse<List<JobFairParticipateDTO>> listBasePagingResponse = jobFairParticipateProvider.searchJobFairParticipate(searchRequest);
                List<JobFairParticipateDTO> dtos = listBasePagingResponse.getDto();
                if (CollectionUtils.isNotEmpty(dtos)) {
                    for (JobFairParticipateDTO dto : dtos) {
                        CompanyDTO cachedCompanyInfo = companyProvider.getCachedCompanyInfo(dto.getOrgId());
                        if (cachedCompanyInfo != null && StrUtil.isNotBlank(cachedCompanyInfo.getId())) {
                            dto.setLogoRelativeUri(cachedCompanyInfo.getLogoRelativeUri());
                            dto.setIndustryClassify(cachedCompanyInfo.getIndustryClassify());
                            dto.setUnitSize(cachedCompanyInfo.getUnitSize());
                            dto.setLegalAddress(cachedCompanyInfo.getLegalAddress());
                            dto.setOrgName(cachedCompanyInfo.getName());
                        }
                    }
                }

                BeanUtil.copyProperties(listBasePagingResponse, basePagingResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_SEARCH;
            }
        });

        return basePagingResponse;
    }


    @Override
    public BaseResponse<JobFairParticipateDTO> getJobFairOrgParticipate(final BaseGetRequest baseGetRequest) {

        final BaseResponse<JobFairParticipateDTO> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(baseGetRequest, "获取单条JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) baseGetRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                final BaseResponse<JobFairParticipateDTO> jobFairParticipateResponse = jobFairParticipateProvider.getJobFairParticipate(baseGetRequest);
                BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);

            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_GET;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<String> insertJobFairOrgParticipate(final JobFairParticipateInsertRequest insertRequest) {

        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getJobFairId(), "jobFairId不能为空");
            }

            @Override
            public void executeService() {
                insertRequest.setCreatorType(UserTypeEnum.COMPANY_USER.name());
                insertRequest.setCreatorId(SessionUtils.getCompanyUserId());

                CompanyDTO companyInfo = userProvider.getCompanyInfo();
                insertRequest.setOrgId(companyInfo.getId());
                insertRequest.setOrgType(OrgTypeEnum.COMPANY.name());
                insertRequest.setOrgName(companyInfo.getName());
                insertRequest.setAuditStatus(JobCheckStatusEnum.AUDIT_WAIT.name());

                BaseResponse<String> jobFairParticipateResponse = jobFairParticipateProvider.insertJobFairParticipate(insertRequest);
                BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> insertManagerJobFairOrgParticipate(JobFairParticipateInsertRequest insertRequest) {
        final BaseResponse<String> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(insertRequest, "新增JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotNull(insertRequest.getJobFairId(), "jobFairId不能为空");
            }

            @Override
            public void executeService() {
                insertRequest.setCreatorType(UserTypeEnum.GOV_EMPLOYEE.name());
                insertRequest.setCreatorId(SessionUtils.getGovUserId());
                insertRequest.setOrgType(OrgTypeEnum.COMPANY.name());
                insertRequest.setAuditStatus(JobCheckStatusEnum.SUCCESS.name());
                JobRecruitPositionInsertRequest position = new JobRecruitPositionInsertRequest();
                BeanUtil.copyProperties(insertRequest, position);
                position.setFairId(insertRequest.getJobFairId());
                position.setName(insertRequest.getName());
                position.setExpireTime(insertRequest.getExpireTime());
                position.setMode(FairModeEnum.ONLINE.toString());
//                if (ObjectUtil.isNotEmpty(insertRequest.getOrgId())){
//                    if (ObjectUtil.isNotEmpty(insertRequest.getRecruitPositionId())){
//                        //更新职位
//                        JobRecruitPositionUpdateRequest updposition = new JobRecruitPositionUpdateRequest();
//                        BeanUtil.copyProperties(insertRequest,updposition);
//                        updposition.setId(insertRequest.getRecruitPositionId());
//                        updposition.setFairId(insertRequest.getJobFairId());
//                        jobRecruitPositionConsumer.updateJobReportRecruitPosition(updposition);
//                    }else {
//                        //新增职位
//                        jobRecruitPositionConsumer.insertJobReportRecruitPosition(position);
//                    }
//                }
//                else {
//                    //新增职位
//                    jobRecruitPositionConsumer.insertJobReportRecruitPosition(position);
//                }
                BaseResponse<JobFairParticipateDTO> participate = jobFairParticipateProvider.getParticipateByOrgId(insertRequest.getOrgId());
                //修改参会企业
                if (StrUtil.isNotBlank(insertRequest.getOrgId()) && participate.getDto() != null) {
                    JobFairParticipateUpdateRequest updateRequest = new JobFairParticipateUpdateRequest();
                    BeanUtils.copyProperties(insertRequest, updateRequest);
                    updateRequest.setId(insertRequest.getOrgId());
                    updateRequest.setModifierId(insertRequest.getCreatorId());
                    updateRequest.setModifierType(insertRequest.getCreatorType());
                    updateRequest.setModifyDatetime(insertRequest.getCreateDatetime());
                    BaseResponse<Integer> jobFairParticipateResponse = jobFairParticipateProvider.updateJobFairParticipate(updateRequest);
                    position.setOrgId(insertRequest.getOrgId());
                    jobRecruitPositionConsumer.insertJobReportRecruitPosition(position);
                    BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
                    //新增参会企业
                } else {
                    String orgId = System.currentTimeMillis() + ""; //todo id生成
                    if (StrUtil.isBlank(insertRequest.getOrgId())) {
                        insertRequest.setOrgId(orgId);
                    } else {
                        insertRequest.setOrgId(insertRequest.getOrgId());
                    }
                    BaseResponse<String> jobFairParticipateResponse = jobFairParticipateProvider.insertJobFairParticipate(insertRequest);
                    position.setOrgId(orgId);
                    jobRecruitPositionConsumer.insertJobReportRecruitPosition(position);
                    BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
                }

            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_INSERT;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<String> reApplyJobFair(JobFairParticipateUpdateRequest updateRequest) {

        BizCheckUtil.checkNotNull(updateRequest, "新增JobFairParticipate的请求参数不能为空");
        BizCheckUtil.checkNotNull(updateRequest.getJobFairId(), "jobFairId不能为空");

        updateRequest.setModifierType(UserTypeEnum.COMPANY_USER.name());
        updateRequest.setModifierId(SessionUtils.getCompanyUserId());

        CompanyDTO companyInfo = userProvider.getCompanyInfo();
        updateRequest.setOrgId(companyInfo.getId());
        updateRequest.setOrgType(OrgTypeEnum.COMPANY.name());
        updateRequest.setOrgName(companyInfo.getName());
        updateRequest.setAuditStatus(JobCheckStatusEnum.AUDIT_WAIT.name());
        return jobFairParticipateProvider.reApplyJobFair(updateRequest);
    }


    @Override
    public BaseResponse<Integer> updateJobFairOrgParticipate(final JobFairParticipateUpdateRequest updateRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                BaseResponse<Integer> jobFairParticipateResponse = jobFairParticipateProvider.updateJobFairParticipate(updateRequest);
                BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_UPDATE;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> updateManagerJobFairOrgParticipate(JobFairParticipateUpdateRequest updateRequest) {
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(updateRequest, "修改JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank(updateRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                updateRequest.setModifierType(UserTypeEnum.COMPANY_USER.name());
                updateRequest.setModifierId(SessionUtils.getGovUserId());
                updateRequest.setModifyDatetime(new Date());
                updateRequest.setAuditStatus(JobCheckStatusEnum.SUCCESS.name());//pc端新增直接审核通过状态

//                JobRecruitPositionInsertRequest position = new JobRecruitPositionInsertRequest();
//                BeanUtil.copyProperties(updateRequest, position);
//                position.setFairId(updateRequest.getJobFairId());
//                position.setFairId(updateRequest.getJobFairId());
//                position.setName(updateRequest.getName());
//                position.setExpireTime(updateRequest.getExpireTime());
//                position.setMode(FairModeEnum.ONLINE.getDesc());

//                if (ObjectUtil.isNotEmpty(updateRequest.getOrgId())) {
//                    if (ObjectUtil.isNotEmpty(updateRequest.getRecruitPositionId())) {
//                        //更新职位
//                        JobRecruitPositionUpdateRequest updposition = new JobRecruitPositionUpdateRequest();
//                        BeanUtil.copyProperties(updateRequest, updposition);
//                        updposition.setId(updateRequest.getRecruitPositionId());
//                        updposition.setFairId(updateRequest.getJobFairId());
//                        jobRecruitPositionConsumer.updateJobReportRecruitPosition(updposition);
//                    } else {
//                        //新增职位
//                        jobRecruitPositionConsumer.insertJobReportRecruitPosition(position);
//                    }
//                } else {


//                BaseDeleteRequest baseDeleteRequest = new BaseDeleteRequest();
//                baseDeleteRequest.setId(updateRequest.getPositionId());
//                jobRecruitPositionConsumer.deleteJobRecruitPosition(baseDeleteRequest);
//                position.setOrgId(updateRequest.getId());
//                jobRecruitPositionConsumer.insertJobReportRecruitPosition(position);

                BaseResponse<Integer> jobFairParticipateResponse = jobFairParticipateProvider.updateJobFairParticipate(updateRequest);
                BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_UPDATE;
            }
        });
        return baseResponse;
    }


    @Override
    public BaseResponse<Integer> deleteJobFairOrgParticipate(final BaseDeleteRequest deleteRequest) {

        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getModifierId(), "操作人不能为空");
                BizCheckUtil.checkNotBlank(deleteRequest.getModifierType(), "操作人类型不能为空");
            }

            @Override
            public void executeService() {
                BaseResponse<Integer> jobFairParticipateResponse = jobFairParticipateProvider.deleteJobFairParticipate(deleteRequest);
                BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> deleteManagerJobFairOrgParticipate(BaseDeleteRequest deleteRequest) {
        final BaseResponse<Integer> baseResponse = new BaseResponse<>();
        serviceTemplate.execute(transactionTemplate, baseResponse, new ServiceCallback() {
            @Override
            public void check() {
                BizCheckUtil.checkNotNull(deleteRequest, "逻辑删除JobFairParticipate的请求参数不能为空");
                BizCheckUtil.checkNotBlank((String) deleteRequest.getId(), "id不能为空");
            }

            @Override
            public void executeService() {
                deleteRequest.setModifierType(UserTypeEnum.COMPANY_USER.name());
                deleteRequest.setModifierId(SessionUtils.getGovUserId());
                deleteRequest.setModifyDatetime(new Date());
                BaseResponse<Integer> jobFairParticipateResponse = jobFairParticipateProvider.deleteJobFairParticipate(deleteRequest);
                BeanUtil.copyProperties(jobFairParticipateResponse, baseResponse);
            }

            @Override
            public ScenarioCodeEnum getScenarioEnum() {
                return ScenarioCodeEnum.JOB_FAIR_PARTICIPATE_DELETE_LOGICALLY;
            }
        });
        return baseResponse;
    }

    @Override
    public BaseResponse<Integer> checkFairRecruitPosition(JobFairParticipateUpdateRequest jobFairRecruitPositionUpdateRequest) {

        return jobFairParticipateProvider.checkFairRecruitPosition(jobFairRecruitPositionUpdateRequest);
    }
}
